jQuery(document).ready(function() {

	//DD_roundies.addRule('#goalrank', '5px', true);
	//DD_roundies.addRule('.bogoc', '5px', true);

	jQuery('#user_intro:not(div#user_intro[rel=nochange])').editable(
			function(value, settings) {
				UserManagerExt.saveAboutMe(value);
				return (jQuery.trim(value));
			}, 
			{
				cssclass : 'editabletext',
				type : 'textarea',
				onblur: 'submit',				
				maxlength: 500,
				name: 'elastic',
				height: '30px',
				maxHeight: '215px',
				data : function(value, settings) {
					value = jQuery.trim(value);
					return value;
			    },
				afterFormRenderedCallback : function() {
		        }
			}
	);	
	
	var $activities = jQuery("div#activities"); 	
	var $activitiesBox = jQuery("ul#activitiesBox");
	var _exceptForMeIndicate = $activitiesBox.attr("rel");
	var _exceptForMe = false;
	if(_exceptForMeIndicate == "yes"){
		_exceptForMe = true;
	}
	var _url = ctx+'/ajax/activities.html?lastOrderValue=';
	var $loader = $activities.find(".loading");
	var $nextPage = $activities.find("a#next_page");
	var loading = false;
	
	$nextPage.click(function(){
		loadActivities();
		return false;
	});
	
	function loadActivities(lastValue){		
		var lastOrderValue = $activities.find("li:last").attr("id");
		if(lastValue != undefined){
			lastOrderValue = lastValue;
		}
		
		var username = $activities.attr("rel");
		if(loading == true)
			return false;
		
		loading = true;
		jQuery.ajax({
			url: _url + lastOrderValue + "&username=" + username + "&exceptForMe=" + _exceptForMe,
			success: function(result){
				$loader.hide();				
				loading = false;
				if(jQuery.trim(result) != "none"){					
					$activitiesBox.html("").append(result).slideDown();
				}else{
//					$nextPage.fadeOut();
					loadActivities(-1);
				}
				
			},
			beforeSend: function(){
				$activitiesBox.hide();
				$loader.show();
			},
			error: function(){
				$nextPage.fadeOut();
				loadActivities(-1);
			}
		});
	}
});